package com.erpanderp.wam.cm.view;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.AjaxBehaviorEvent;
import javax.servlet.http.HttpSession;

import org.primefaces.component.datatable.DataTable;
import org.primefaces.context.RequestContext;

import com.erpanderp.wam.cm.model.entity.AdditionalSkill;
import com.erpanderp.wam.cm.model.entity.EmployeeSkillMatrix;
import com.erpanderp.wam.cm.model.entity.SAPCertification;
import com.erpanderp.wam.cm.model.service.EmployeeSkillDAO;
import com.erpanderp.wam.usm.model.entity.user.Employee;
import com.erpanderp.wam.usm.model.entity.user.WAMUser;
import com.erpanderp.wam.usm.model.service.user.EmployeeDAO;
import com.erpanderp.wam.util.CommonLookup;
import com.erpanderp.wam.util.ValidationUtil;

@ManagedBean (name="skillMatrixBean")
@ViewScoped
public class EmployeeSkillsBean implements Serializable{

	private static final long serialVersionUID = 1L;
	HttpSession session = (HttpSession) FacesContext.getCurrentInstance()
			.getExternalContext().getSession(true);
	private TimeZone timeZone = TimeZone.getDefault();
	private WAMUser wamUser;
	private boolean FORM_VALID;
	private boolean POSITIVENUMBER_VALID;
	private boolean ADDITIONALSKILL_VALID;
	private boolean EMPLOYEE_AVAILABLE;	
	
	private Employee employee = new Employee();
	private SAPCertification sapCertification = new SAPCertification();
	private AdditionalSkill addtionalSkill =new AdditionalSkill();
	private EmployeeSkillMatrix employeeSkillMatrix = new EmployeeSkillMatrix();
	private EmployeeSkillDAO employeeSkillDAO =  new EmployeeSkillDAO();
	private EmployeeDAO employeeDAO = new EmployeeDAO();
		
	private List<SAPCertification> sapCertifications = new ArrayList<SAPCertification>();
	private List<AdditionalSkill> additionalSAPSkills = new ArrayList<AdditionalSkill>();
	
	private DataTable assignedAdditionalSkillsTable;
	private DataTable assignedSAPCertificationsTable;
	private Map<String,String> additioanlSkillsMap ;	
	
	private String employeeName;
	private String employeeId;
	private String designation;
	private String qualification;
	private String contactNumber;
	private String lastUpdateOn;
	
	private Float sapExperience;
	private Float endUserExperience;
	private Float nonSAPExperience;
	

	//Default Constructor
	public EmployeeSkillsBean() {
		
		setWamUser((WAMUser) session.getAttribute(CommonLookup.SESSION_ATTRIBUTE_WAMUSER));
		String fullName = wamUser.getEmployee().getFirstName()+ " "+wamUser.getEmployee().getLastName() ;
		
		setEmployeeName(fullName);
		setEmployeeId(wamUser.getEmployee().getEmployeeCode());
		setDesignation(wamUser.getEmployee().getDesignation());
		setContactNumber(wamUser.getEmployee().getTelephone());
		
		setAdditioanlSkillsMap(CommonLookup.getSkillMatrixAdditionalSkills());
		
		System.out.println("wamUser.getEmployee().getEmployeeId()===========EMP ID===="+wamUser.getEmployee().getEmployeeId());
		
		EMPLOYEE_AVAILABLE = employeeSkillDAO. isEmployeeAvailable(wamUser.getEmployee().getEmployeeCode());
		
		System.out.println("EMPLOYEE_AVAILABLE============T/F====="+EMPLOYEE_AVAILABLE);
		if(EMPLOYEE_AVAILABLE){
			System.out.println("===============INSIDE IF ==============="+wamUser.getEmployee().getEmployeeCode());
			employeeSkillMatrix = employeeSkillDAO.getEmployeeSkillMatrixByEmployeeCode(wamUser.getEmployee().getEmployeeCode());
			System.out.println("===============11111============after===");
			System.out.println("===============11111============after==="+employeeSkillMatrix);
			//sapCertifications.addAll(employeeSkillMatrix.getSapCertifications());
			//additionalSAPSkills.addAll(employeeSkillMatrix.getAdditionalSkills());
			//System.out.println("employeeSkillMatrix.getSapCertifications()==COUNT======"+employeeSkillMatrix.getSapCertifications().size());
			sapCertifications = employeeSkillDAO.getAllSAPCertificationsBySkillMatrixID(employeeSkillMatrix.getEmployeeSkillMatrixId());
			additionalSAPSkills = employeeSkillDAO.getAllAdditionalSkillsBySkillMatrixID(employeeSkillMatrix.getEmployeeSkillMatrixId());
			getTotalExperience(null);
		
		}
		
		
		
	}
	
	// ACTIONS FOR CREATING NEW CERTIFICATION
		public String createAnotherCertificationAction() {
			System.out.println("createAnotherCertificationAction...............");
			validateForm();

			if (FORM_VALID) {
				
				sapCertification.setCreateDate(new Date());
				sapCertification.setEmployeeSkill(employeeSkillMatrix);
				sapCertification.setCreatedBy(CommonLookup.CREATEDBY_DEVELOPER_TEXT);
				sapCertification.setStatus(CommonLookup.STATUS_ACTIVE);
				sapCertifications.add(sapCertification);
				sapCertification = new SAPCertification();
			}

			return null;
		}
	// ACTIONS FOR CREATING Additional SAP Skills
		public void createAnotherSAPSkillsAction(ActionEvent e) {
			System.out.println("createAnotherSAPSkillsAction...............");
			validateSAPAdditionalSkillsForm();

			if (ADDITIONALSKILL_VALID) {
				
				addtionalSkill.setCreateDate(new Date());	
				addtionalSkill.setEmployeeSkill(employeeSkillMatrix);
				addtionalSkill.setCreatedBy(CommonLookup.CREATEDBY_DEVELOPER_TEXT);
				addtionalSkill.setStatus(CommonLookup.STATUS_ACTIVE);
				additionalSAPSkills.add(addtionalSkill);
				addtionalSkill = new AdditionalSkill();
			}			
		}
		
	public String saveSkillMatrixAction() {
			
			try{
				
				if (sapCertifications.size() > 0 ) {
					employeeSkillDAO.saveSAPCertifications(sapCertifications);
				}
				
				if (additionalSAPSkills.size() > 0 ) {
					employeeSkillDAO.saveAdditionalSkills(additionalSAPSkills);
				}

				employeeSkillMatrix.setCreateDate(new Date());
				employeeSkillMatrix.setCreatedBy(CommonLookup.CREATEDBY_DEVELOPER_TEXT);
				employeeSkillMatrix.setStatus(CommonLookup.STATUS_ACTIVE);
				employeeSkillMatrix.setEmployee(wamUser.getEmployee());
				
				employeeSkillDAO.saveEmployeeSkillMatrix(employeeSkillMatrix);
				
					
				FacesContext.getCurrentInstance().addMessage("", new FacesMessage(FacesMessage.SEVERITY_INFO, "your skill matrix updated successfully.", ""));
			}catch (Exception e) {
				e.printStackTrace();
				FacesContext.getCurrentInstance().addMessage("", new FacesMessage(FacesMessage.SEVERITY_ERROR, "Failed to update Skill Matrix.", e.getMessage()));
			}
		
			return null;
		}
	public void getTotalExperience(AjaxBehaviorEvent e){
		sapExperience = employeeSkillMatrix.getSapExperience();
		endUserExperience = employeeSkillMatrix.getSapEndUserExperience();
		nonSAPExperience = employeeSkillMatrix.getNonSAPExperience();
		
		positiveNumberValidation();
		if(POSITIVENUMBER_VALID){
		System.out.println("sapExperience::::::::"+sapExperience);
		System.out.println("endUserExperience ::::::::"+endUserExperience);
		System.out.println("nonSAPExperience ::::::::"+nonSAPExperience);
		
		if(sapExperience == null || sapExperience == 0.0){
			sapExperience = 0.0f;
		}
		if(endUserExperience == null || endUserExperience == 0.0){
			endUserExperience = 0.0f;
		}
		if(nonSAPExperience == null || nonSAPExperience == 0.0){
			nonSAPExperience = 0.0f;
		}
		
		System.out.println("TOTAL EXPERIENCE ::::::::"+(sapExperience+endUserExperience+nonSAPExperience));
		employeeSkillMatrix.setTotalExperience(sapExperience+endUserExperience+nonSAPExperience);
		}
		
	}
	
	public void removeSkill(ActionEvent e){
		AdditionalSkill selectedskill = (AdditionalSkill) assignedAdditionalSkillsTable.getRowData();
		employeeSkillDAO.deleteAdditionalSkillById(selectedskill.getAdditionalSkillId());
		additionalSAPSkills.remove(selectedskill);	
	}
	
	public void removeCertification(ActionEvent e){
		SAPCertification selectedCertification = (SAPCertification) assignedSAPCertificationsTable.getRowData();
		employeeSkillDAO.deleteSAPCerificationById(selectedCertification.getSapCertificationId());		
		sapCertifications.remove(selectedCertification);
	}
	
	// VALIDATIONS
		private void validateForm() {
			FORM_VALID = true;

			if (ValidationUtil.isEmpty(sapCertification.getCertificationName(), "Certification Name",
					null, FacesContext.getCurrentInstance()) != null) {
				FORM_VALID = false;
			}
			if (ValidationUtil.isEmpty(sapCertification.getVersion(), " Certification Version", null,
					FacesContext.getCurrentInstance()) != null) {
				FORM_VALID = false;
			}
		}
	
	//POSITIVE NUMBER VALIDATION
	private void positiveNumberValidation(){
		POSITIVENUMBER_VALID =true;
		
		if (ValidationUtil.isEmpty(employeeSkillMatrix.getSapExperience().toString(), "SAP Experience", null,
				FacesContext.getCurrentInstance()) != null) {
			employeeSkillMatrix.setSapExperience(0.0f);
		}
		if (ValidationUtil.isValidPositiveFloatNumber(sapExperience.toString(), "SAP Experience", null,
				FacesContext.getCurrentInstance()) != null) {
			POSITIVENUMBER_VALID = false;
			employeeSkillMatrix.setSapExperience(0.0f);
		} 
		
		
		if (ValidationUtil.isEmpty(employeeSkillMatrix.getSapEndUserExperience().toString(), "SAP End User Experience", null,
				FacesContext.getCurrentInstance()) != null) {
			employeeSkillMatrix.setSapEndUserExperience(0.0f);
		}
		if (ValidationUtil.isValidPositiveFloatNumber(endUserExperience.toString(), "SAP End User Experience", null,
				FacesContext.getCurrentInstance()) != null) {
			POSITIVENUMBER_VALID = false;
			employeeSkillMatrix.setSapEndUserExperience(0.0f);
		} 
		
		if (ValidationUtil.isEmpty(employeeSkillMatrix.getNonSAPExperience().toString(), "Non-SAP/Domain Experience", null,
				FacesContext.getCurrentInstance()) != null) {
			employeeSkillMatrix.setNonSAPExperience(0.0f);
		}
		if (ValidationUtil.isValidPositiveFloatNumber(nonSAPExperience.toString(), "Non-SAP/Domain Experience", null,
				FacesContext.getCurrentInstance()) != null) {
			POSITIVENUMBER_VALID = false;
			employeeSkillMatrix.setNonSAPExperience(0.0f);
		} 
		
	}
		
		private void validateSAPAdditionalSkillsForm() {
			ADDITIONALSKILL_VALID=true;

			if (ValidationUtil.isEmpty(addtionalSkill.getAdditionalSkill(), "Skill Name",
					null, FacesContext.getCurrentInstance()) != null) {
				ADDITIONALSKILL_VALID = false;
			}			
			if (ValidationUtil.isEmpty(String.valueOf(addtionalSkill.getNoOfProject()), "No of Projects", null,
					FacesContext.getCurrentInstance()) != null) {
				ADDITIONALSKILL_VALID  = false;
			}
			if(addtionalSkill.getNoOfProject() == 0){
				FacesContext.getCurrentInstance().addMessage("", new FacesMessage(FacesMessage.SEVERITY_WARN, "No of Projects count should not zero.", ""));
				ADDITIONALSKILL_VALID  = false;
			}
		}
		
		/*public void confirmationAction(ActionEvent e){
			validateForm();	
			if(FORM_VALID){	
				RequestContext.getCurrentInstance().execute("confirmation.show();");
			}else {
				RequestContext.getCurrentInstance().execute("confirmation.hide();");
			}
	    }	*/
	//GENERATE SETTERS AND GETTERS
	public WAMUser getWamUser() {
		return wamUser;
	}

	public void setWamUser(WAMUser wamUser) {
		this.wamUser = wamUser;
	}

	public Employee getEmployee() {
		return employee;
	}

	public void setEmployee(Employee employee) {
		this.employee = employee;
	}

	public String getEmployeeName() {
		return employeeName;
	}

	public void setEmployeeName(String employeeName) {
		this.employeeName = employeeName;
	}

	public String getEmployeeId() {
		return employeeId;
	}

	public void setEmployeeId(String employeeId) {
		this.employeeId = employeeId;
	}

	public String getDesignation() {
		return designation;
	}

	public void setDesignation(String designation) {
		this.designation = designation;
	}

	public String getQualification() {
		return qualification;
	}

	public void setQualification(String qualification) {
		this.qualification = qualification;
	}

	public String getContactNumber() {
		return contactNumber;
	}

	public void setContactNumber(String contactNumber) {
		this.contactNumber = contactNumber;
	}

	public String getLastUpdateOn() {
		return lastUpdateOn;
	}

	public void setLastUpdateOn(String lastUpdateOn) {
		this.lastUpdateOn = lastUpdateOn;
	}

	public boolean isFORM_VALID() {
		return FORM_VALID;
	}

	public void setFORM_VALID(boolean fORM_VALID) {
		FORM_VALID = fORM_VALID;
	}

	public SAPCertification getSapCertification() {
		return sapCertification;
	}

	public void setSapCertification(SAPCertification sapCertification) {
		this.sapCertification = sapCertification;
	}

	public AdditionalSkill getAddtionalSkill() {
		return addtionalSkill;
	}

	public void setAddtionalSkill(AdditionalSkill addtionalSkill) {
		this.addtionalSkill = addtionalSkill;
	}

	public EmployeeSkillMatrix getEmployeeSkillMatrix() {
		return employeeSkillMatrix;
	}

	public void setEmployeeSkillMatrix(EmployeeSkillMatrix employeeSkillMatrix) {
		this.employeeSkillMatrix = employeeSkillMatrix;
	}

	public List<SAPCertification> getSapCertifications() {
		return sapCertifications;
	}

	public void setSapCertifications(List<SAPCertification> sapCertifications) {
		this.sapCertifications = sapCertifications;
	}

	public List<AdditionalSkill> getAdditionalSAPSkills() {
		return additionalSAPSkills;
	}

	public void setAdditionalSAPSkills(List<AdditionalSkill> additionalSAPSkills) {
		this.additionalSAPSkills = additionalSAPSkills;
	}

	public boolean isADDITIONALSKILL_VALID() {
		return ADDITIONALSKILL_VALID;
	}

	public void setADDITIONALSKILL_VALID(boolean aDDITIONALSKILL_VALID) {
		ADDITIONALSKILL_VALID = aDDITIONALSKILL_VALID;
	}

	public EmployeeDAO getEmployeeDAO() {
		return employeeDAO;
	}

	public void setEmployeeDAO(EmployeeDAO employeeDAO) {
		this.employeeDAO = employeeDAO;
	}

	public boolean isEMPLOYEE_AVAILABLE() {
		return EMPLOYEE_AVAILABLE;
	}

	public void setEMPLOYEE_AVAILABLE(boolean eMPLOYEE_AVAILABLE) {
		EMPLOYEE_AVAILABLE = eMPLOYEE_AVAILABLE;
	}
	
	public DataTable getAssignedAdditionalSkillsTable() {
		return assignedAdditionalSkillsTable;
	}

	public void setAssignedAdditionalSkillsTable(
			DataTable assignedAdditionalSkillsTable) {
		this.assignedAdditionalSkillsTable = assignedAdditionalSkillsTable;
	}

	public DataTable getAssignedSAPCertificationsTable() {
		return assignedSAPCertificationsTable;
	}

	public void setAssignedSAPCertificationsTable(
			DataTable assignedSAPCertificationsTable) {
		this.assignedSAPCertificationsTable = assignedSAPCertificationsTable;
	}

	public Map<String,String> getAdditioanlSkillsMap() {
		return additioanlSkillsMap;
	}

	public void setAdditioanlSkillsMap(Map<String,String> additioanlSkillsMap) {
		this.additioanlSkillsMap = additioanlSkillsMap;
	}

	public Float getSapExperience() {
		return sapExperience;
	}

	public void setSapExperience(Float sapExperience) {
		this.sapExperience = sapExperience;
	}

	public Float getEndUserExperience() {
		return endUserExperience;
	}

	public void setEndUserExperience(Float endUserExperience) {
		this.endUserExperience = endUserExperience;
	}

	public Float getNonSAPExperience() {
		return nonSAPExperience;
	}

	public void setNonSAPExperience(Float nonSAPExperience) {
		this.nonSAPExperience = nonSAPExperience;
	}

	public TimeZone getTimeZone() {
		return timeZone;
	}

	public void setTimeZone(TimeZone timeZone) {
		this.timeZone = timeZone;
	}


}
